package com.daka.controller;

import com.daka.entity.GroupVolunteer;
import com.daka.entity.Volunteer;
import com.daka.enumerate.StatusCode;
import com.daka.service.GroupVolunteerService;
import com.daka.service.VolunteerService;
import com.daka.tools.DKQConstant;
import com.daka.vo.response.BaseResponseVo;
import com.daka.vo.response.DataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.*;

/**
 * Created by yangx
 * Date: 2016/8/3 0003
 * Time: 9:28
 */

@Controller
@RequestMapping("/groupVolunteer")
@Slf4j
public class GroupVolunteerController {

    @Autowired
    private GroupVolunteerService groupVolunteerService;
    @Autowired
    private VolunteerService volunteerService;


    /**
     * 管理员列表
     * @param groupId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryManager")
    public DataResponseVo<Object> queryManger(@RequestParam(value = "groupId")String groupId) {
        try{
            List<Volunteer> list = volunteerService.queryManager(Long.valueOf(groupId));
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),list);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }

    }

    /**
     * //设置管理员；修改志愿者状态
     *  1、groupId和volunteerId必传
     *
     * @param groupVolunteer
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update")
    public DataResponseVo<Object> manger(GroupVolunteer groupVolunteer) {
        try {
            //groupId和volunteerId必传
            if (groupVolunteer.getGroupId() == null || groupVolunteer.getVolunteerId() == null) {
                return new DataResponseVo<Object>(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
            }
            GroupVolunteer groupVolunteerParam =new GroupVolunteer();
            groupVolunteerParam.setGroupId(groupVolunteer.getGroupId());
            groupVolunteerParam.setVolunteerId(groupVolunteer.getVolunteerId());
            List<GroupVolunteer> list = groupVolunteerService.queryByEntity(groupVolunteerParam);
            if (list == null || list.size() != 1) {
                return new DataResponseVo<Object>(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
            }
            GroupVolunteer groupVolunteer1 = list.get(0);
            //判断role和status
            if (groupVolunteer.getRole() != null) {
                if (groupVolunteer.getRole() == 0 || groupVolunteer.getRole() == 1) {
                    groupVolunteer1.setRole(groupVolunteer.getRole());
                }
            } else if (groupVolunteer.getStatus() != null) {
                groupVolunteer1.setStatus(groupVolunteer.getStatus());
            } else {
                return new DataResponseVo<Object>(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
            }

            groupVolunteerService.update(groupVolunteer1);
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }


    /**
     * 新增普通志愿者
     * @param groupVolunteer
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/approve")
    public BaseResponseVo approve(GroupVolunteer groupVolunteer) {
        try {
            if (groupVolunteer.getVolunteerId() == 0 || groupVolunteer.getVolunteerId() ==null
                    || groupVolunteer.getGroupId() == 0|| groupVolunteer.getGroupId() == null) {
                return new BaseResponseVo(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
            }
            GroupVolunteer groupVolunteerParam =new GroupVolunteer();
            groupVolunteerParam.setGroupId(groupVolunteer.getGroupId());
            groupVolunteerParam.setVolunteerId(groupVolunteer.getVolunteerId());
            List<GroupVolunteer> list1 = groupVolunteerService.queryByEntity(groupVolunteerParam);
            if(list1!=null || list1.size()<1){
                return new BaseResponseVo(StatusCode.FILE_EXIT.getCode(),StatusCode.FILE_EXIT.getMessage());
            }

            //通过
            groupVolunteer.setCreateTime(new Timestamp(new Date().getTime()));
            groupVolunteer.setStatus(DKQConstant.APPLY_STATUS_OK);
            groupVolunteer.setRole(DKQConstant.ROLES_VOLUNTEER);
            groupVolunteer.setTimes(0l);
            groupVolunteer.setDelStatus(0);
            groupVolunteerService.insert(groupVolunteer);
            return new BaseResponseVo(StatusCode.SUCCESS.getCode(),StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new BaseResponseVo(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }



}


















